package com.future.mayi.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.activity.GoodsActivity;
import com.future.mayi.model.GoodsDetailModel;
import com.future.mayi.model.IndexTimeModel;
import com.future.mayi.model.interfaces.IndexTimeListener;
import com.future.mayi.utils.PriceUtils;
import com.future.mayi.view.IndexTimeView;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class IndexGoodsAdapter extends BaseRecyclerViewAdapter<GoodsDetailModel> implements IndexTimeListener {


    private int imgHeight = 0;
    private int newMargin = 0;

    public IndexGoodsAdapter(Context context, XRecyclerView recyclerView) {
        super(context, null, recyclerView);
        imgHeight = (int) (((float) CommonUtils.getScreenSize(context)[0] - CommonUtils.dip2px(context, 12 * 2)) * (169.0f / 351.0f));
        newMargin = imgHeight - CommonUtils.dip2px(mContext, 8);
    }

    @Override
    public int getItemViewType(int position) {
        if (getItem(position) != null && getItem(position).getTimeModel() != null) {
            return 99;
        }
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 99) return new TimeViewHolder(new IndexTimeView(parent.getContext()));
        return new ViewHolder(getView(parent, R.layout.layout_index_active_item));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {

        if (viewHolder instanceof TimeViewHolder) {
            TimeViewHolder holder = (TimeViewHolder) viewHolder;
            holder.timeView.setModel(getItem(position).getTimeModel(), this);
            return;
        }

        ViewHolder holder = (ViewHolder) viewHolder;
        GoodsDetailModel model = getItem(position);
        setText(holder.tvTitle, model.getGoods_name());
        setText(holder.tvSubTitle, model.getSubtitle());
        setText(holder.tvPrice, "¥" + PriceUtils.getPriceText2(model.getPrice()));
        setText(holder.tvViews, String.valueOf(model.getSales()));
        loadImg(holder.ivImg, model.getImage());
        holder.tvNew.setVisibility(model.getIs_new() == 1 ? View.VISIBLE : View.GONE);

        if (model.getRebate_price() > 0 || model.getMin_rebate_price() > 0 || model.getMax_rebate_price() > 0) {
            String rabate;
            if (model.getMin_rebate_price() > 0 || model.getMax_rebate_price() > 0) {
                if (model.getMax_rebate_price() == model.getMin_rebate_price()) {
                    rabate = PriceUtils.getPriceText2(model.getMin_rebate_price());
                } else {
                    rabate = PriceUtils.getPriceText2(model.getMin_rebate_price()) + "-" + PriceUtils.getPriceText2(model.getMax_rebate_price());
                }
            } else {
                rabate = PriceUtils.getPriceText2(model.getRebate_price());
            }
            holder.llZhuan.setVisibility(View.VISIBLE);
            setText(holder.tvZhuanPrice, rabate);
        } else {
            holder.llZhuan.setVisibility(View.GONE);
        }

        holder.ivImg.getLayoutParams().height = imgHeight;
        ((RelativeLayout.LayoutParams) holder.tvNew.getLayoutParams()).topMargin = newMargin;
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GoodsActivity.startActivity(mContext, getItem(position).getGoods_id());
            }
        });
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_img)
        ImageView ivImg;
        @BindView(R.id.tv_new)
        TextView tvNew;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_sub_title)
        TextView tvSubTitle;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_views)
        TextView tvViews;
        @BindView(R.id.ll_zhuan)
        View llZhuan;
        @BindView(R.id.tv_zhuan_price)
        TextView tvZhuanPrice;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    class TimeViewHolder extends RecyclerView.ViewHolder {

        IndexTimeView timeView;

        public TimeViewHolder(View view) {
            super(view);
            timeView = (IndexTimeView) view;
        }
    }


    @Override
    public void timeEnd(IndexTimeView view, IndexTimeModel model) {

        List<GoodsDetailModel> list = new ArrayList<>(10);
        int timeTag = model.getTimeTag();
        for (GoodsDetailModel item : getList()) {
            if (item.getTimeTag() == timeTag) {
                list.add(item);
            }
        }
        getList().removeAll(list);
        notifyDataSetChanged();
    }
}
